package com.todo.sight.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.sight.application.backend.service.SightPoiBackendService;
import com.todo.sight.application.convert.SightInfoConvert;
import com.todo.sight.application.entity.bo.SightPoiBO;
import com.todo.sight.application.entity.condition.PoiConditionBO;
import com.todo.sight.application.iface.backend.request.*;
import com.todo.sight.application.iface.backend.response.QuerySightPoiByConditionResp;
import com.todo.sight.application.iface.backend.response.QuerySightPoiDetailResp;
import com.todo.sight.application.service.SightPoiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (SightPoi)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("sightPoiBackendService")
public class SightPoiBackendServiceImpl implements SightPoiBackendService {

    @Resource
    private SightPoiService sightPoiService;

    @Override
    public BizResponse<QuerySightPoiDetailResp> querySightPoiById(QuerySightPoiByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightPoiBO sightPoiBO = sightPoiService.querySightPoiById(req.getId());
        if (sightPoiBO == null) {
            log.warn("SightPoiBackendService#querySightPoiById 根据主键查询景点POI详情完成，数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        return BizResponse.success(SightInfoConvert.convertToQuerySightPoiDetailResp(sightPoiBO));
    }

    @Override
    public BizPageResponse<QuerySightPoiByConditionResp> querySightPoiByCondition(QuerySightPoiByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            return BizPageResponse.paramError("参数异常");
        }
        PoiConditionBO poiConditionBO = SightInfoConvert.convertToPoiConditionBO(req);
        PageResult<SightPoiBO> pageResult = sightPoiService.querySightPoiByCondition(poiConditionBO);
        if (pageResult == null) {
            log.warn("SightPoiBackendService#querySightPoiByCondition 多条件查询景点PIO列表完成，数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizPageResponse.success();
        }

        return BizPageResponse.success(SightInfoConvert.convertToQuerySightPoiByConditionResp(pageResult));
    }

    @Override
    public BizResponse<Boolean> insertSightPoi(InsertSightPoiReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightPoiBO sightPoiBO = SightInfoConvert.insertReqConvertToSightPoiBO(req);
        Boolean insertResult = sightPoiService.insertSightPoi(sightPoiBO);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updateSightPoiById(UpdateSightPoiReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightPoiBO alreadySightPoiBO = sightPoiService.querySightPoiById(req.getId());
        if (alreadySightPoiBO == null) {
            log.warn("SightPoiBackendService#updateSightPoiById 根据主键查询景点POI详情完成，历史数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.paramError("数据为空");
        }
        SightPoiBO sightPoiBO = SightInfoConvert.updateReqconvertToSightPoiBO(req);
        Boolean insertResult = sightPoiService.updateSightPoi(sightPoiBO);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> removeSightPoiById(RemoveSightPoiByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        Boolean removeResult = sightPoiService.removeSightPoiById(req.getId());
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<Boolean> enableSightPoiById(EnableSightPoiByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        return null;
    }
}
